//
//  BMPhotoIOTool.m
//  bigmama
//
//  Created by huyuchen on 2017/9/28.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMPhotoIOTool.h"
#import "UIAlertView+BlocksKit.h"

@interface BMPhotoIOTool ()
// ios7读取器
@property (nonatomic, strong) BMPhotoIO_iOS7Tool *photoTool_iOS7;

// ios8以后读取器
@property (nonatomic, strong) BMPhotoIO_iOS8Tool *photoTool;
@end

@implementation BMPhotoIOTool
SingletonM_lib(Tool)

- (instancetype)init
{
    self = [super init];
    if (self) {
        if (IOS8) {
            [self checkAuth];
        } else {
            [self checkAuthWithIOS7];
        }
    }
    return self;
}

#pragma mark - 懒加载
- (NSMutableArray *)albumArray
{
    if (!_albumArray) {
        _albumArray = [NSMutableArray array];
    }
    return _albumArray;
}

- (NSMutableArray *)videoAlbumArr{
    if (!_videoAlbumArr) {
        _videoAlbumArr = [NSMutableArray array];
    }
    return _videoAlbumArr;
}

- (BMPhotoIO_iOS7Tool *)photoTool_iOS7
{
    if (!_photoTool_iOS7) {
        _photoTool_iOS7 = [[BMPhotoIO_iOS7Tool alloc] init];
    }
    return _photoTool_iOS7;
}

- (BMPhotoIO_iOS8Tool *)photoTool
{
    if (!_photoTool) {
        _photoTool = [[BMPhotoIO_iOS8Tool alloc] init];
    }
    return _photoTool;
}

#pragma mark - iOS7
// 授权
- (void)checkAuthWithIOS7
{
    BMWeakSelf
    [self.photoTool_iOS7 checkAuthWithAuthedStatus:^(ALAuthorizationStatus status) {
        // 请求相册
        [weakSelf setupGourpCollectionWithIOS7];
    } denied:^(ALAuthorizationStatus status) {
        
        [UIAlertView bk_showAlertViewWithTitle:@"提示" message:@"需要开启相册权限才能查看照片哦" cancelButtonTitle:@"取消" otherButtonTitles:@[@"去开启"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                if (IOS8) {
                    NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    if ([[UIApplication sharedApplication] canOpenURL:settingURL]) {
                        [[UIApplication sharedApplication] openURL:settingURL];
                    }
                }
            }
        }];
        
    }];
}

// 读取相册
- (void)setupGourpCollectionWithIOS7
{
    BMWeakSelf
    [self.photoTool_iOS7 selectAlbumsWithFinishBlock:^(NSMutableArray *array) {
        weakSelf.albumArray = array;
        weakSelf.albumLoaded = YES;
    }];
    
    [self.photoTool_iOS7 selectVideoAlbumsWithFinishBlock:^(NSMutableArray *array) {
        weakSelf.videoAlbumArr = array;
    }];
}

// 读取相片
- (void)photosWithAssetGroup:(ALAssetsGroup *)group finishBlcok:(void (^)(id obj1, id obj2))finishBlock
{
    [self.photoTool_iOS7 selectPhotosWithAssetGroup:group finishBlcok:finishBlock];
}

// 获取原图
- (UIImage *)originalImageWithAssetInIOS7:(ALAsset *)asset
{
    return [self.photoTool_iOS7 originalImageWithAsset:asset];
}

// 保存图片到相机胶卷
- (void)saveImage:(UIImage *)image metadata:(NSDictionary *)metadata finishedBlock:(void (^)(id obj))finishedBlock
{
    // 这里目前只使用了旧版的保存图片，没有用photoKit
    [self.photoTool_iOS7 saveImage:image metadata:metadata finishedBlock:finishedBlock];
}


#pragma mark - iOS8及以后
// 检查授权
- (void)checkAuth
{
    BMWeakSelf
    [BMPhotoIOAuthTool checkAuthForPhotoWithAuthedStatus:^(PHAuthorizationStatus status) {
        // 初始化相册
        [weakSelf setupGroupCollection];
    } denied:^(PHAuthorizationStatus status) {

    } noRequest:^(PHAuthorizationStatus status) {
        [weakSelf requestAuth];
    }];
}

// 请求授权
- (void)requestAuth
{
    BMWeakSelf
    [BMPhotoIOAuthTool requestAuthForPhotoWithAuthedStatus:^(PHAuthorizationStatus status) {
        // 初始化相册
        [weakSelf setupGroupCollection];
    } denied:^(PHAuthorizationStatus status) {
        [UIAlertView bk_showAlertViewWithTitle:@"提示" message:@"需要开启相册权限才能查看照片哦" cancelButtonTitle:@"取消" otherButtonTitles:@[@"去开启"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                if (IOS8) {
                    NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    if ([[UIApplication sharedApplication] canOpenURL:settingURL]) {
                        [[UIApplication sharedApplication] openURL:settingURL];
                    }
                }
            }
        }];
    }];
}

// 读取相册
- (void)setupGroupCollection
{
    BMWeakSelf
    [self.photoTool selectAlbumWithFinishBlock:^(NSMutableArray *array) {
        weakSelf.albumArray = array;
        weakSelf.albumLoaded = YES;
    } videoBlock:^(NSMutableArray *object) {
        weakSelf.videoAlbumArr = object;
        if (weakSelf.checkAuthorFinishBlock) {
            weakSelf.checkAuthorFinishBlock(object);
        }
    }];
}

// 根据相册获取照片
- (void)photosWithCollection:(PHAssetCollection *)collection finishBlcok:(void (^)(id obj1, id obj2))finishBlock
{
    [self.photoTool selectPhotosWithCollection:collection finishBlcok:^(NSMutableArray *photosArray, id object1) {
        finishBlock(photosArray, object1);
    }];
}

// 获取原图数据data，限制最大容量可通过属性maxDB设置，没设置默认为5MB
- (void)originalImageDataWithAsset:(PHAsset *)asset finishBlock:(void (^)(id obj))finishBLock
{
    [self.photoTool originalImageDataWithAsset:asset finishBlock:finishBLock];
}

- (void)originalImageWithAsset:(PHAsset *)asset finishBlock:(void (^)(id obj))finishBLock
{
    [self.photoTool originalImageWithAsset:asset finishBlock:finishBLock];
}

// 获取缩略图，适合九宫格显示
- (void)thumbImageWithAsset:(PHAsset *)asset finishBlock:(void (^)(id obj))finishBLock
{
    [self.photoTool thumbImageWithAsset:asset finishBlock:finishBLock];
}

// 根据asset获取要缓存的图片，比缩略图大，比原图小，不模糊,适合没上传完显示
- (void)cacheImageWithAsset:(PHAsset *)asset finishBlock:(void (^)(id obj))finishBLock
{
    [self.photoTool cacheImageWithAsset:asset finishBlock:finishBLock];
}

// 根据图片id获取PHAsset对象
- (PHAsset *)getAssetWithID:(NSString *)ID
{
    return [self.photoTool getAssetWithID:ID];
}

#pragma mark - 视频
- (void)videosWithAssetGroup:(ALAssetsGroup *)group finishBlock:(void (^)(id obj1, id obj2))finishBlock{
    [self.photoTool_iOS7 selectVideoWithAssetGroup:group finishBlock:finishBlock];
}

// 根据相册获取视频
- (void)videosWithPhAssetCollection:(PHAssetCollection *)collection finishBlcok:(void (^)(id obj1, id obj2))finishBlock;{
    [self.photoTool selectVideosWithCollection:collection finishBlock:finishBlock];
}

- (void)getAllVideoCollectionFinishBlock:(void (^)(id obj))finishBlock{
    if (self.videoAlbumArr.count) {
        if (finishBlock) {
            finishBlock(self.videoAlbumArr);
            return;
        }
    }
}

- (void)updateVideoCount{
    for (BMPhotoIOAlbumModel *ablumModel in self.videoAlbumArr) {
        [self.photoTool getVideoWithCollection:ablumModel.collection imageBlock:^(UIImage *image) {
            
        } countBlock:^(NSInteger count) {
            ablumModel.count = count;
        }];
    }
}

@end
